package com.webside.advertisement.controller;
 
 
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.camel.model.FromDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.webside.advertisement.model.AdvertisementEntity;
import com.webside.advertisement.service.AdvertisementService;
import com.webside.base.basecontroller.BaseController;
import com.webside.exception.AjaxException;
import com.webside.exception.ServiceException;
import com.webside.exception.SystemException;
import com.webside.listener.BaseData;
import com.webside.listener.vo.Channel;
import com.webside.series.model.Bdictionary;
import com.webside.series.model.SeriesEntity;
import com.webside.series.service.BdictionaryService;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.util.Common;
import com.webside.util.PageUtil;
import com.webside.videoresource.model.VideoResourceEntity;
 

@Controller
@Scope("prototype")
@RequestMapping("/localad/")
public class LocaladController extends BaseController {

	@Autowired
	private  AdvertisementService advertisementService;
	
	@RequestMapping("listUI.html")
	public String listUI(Model model, HttpServletRequest request) {
		try
		{
			PageUtil page = new PageUtil();
			
			
			
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			model.addAttribute("page", page);
			return Common.BACKGROUND_PATH + "/localad/list";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	
	
	
	@RequestMapping("list.html")
	@ResponseBody
	public Object list(String gridPager,HttpServletResponse response) throws Exception{
		Map<String,Object> parameters = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		if (parameters.size() < 0) {
			parameters.put("title", null);
		}//3、判断是否是导出操作
		if(pager.getIsExport())
		{
			if(pager.getExportAllData())
			{
				//3.1、导出全部数据
				List<AdvertisementEntity> list = advertisementService.queryListByPage(parameters);
				ExportUtils.exportAll(response, pager, list);
				return null;
			}else
			{
				//3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		}else
		{
		// 设置分页，page里面包含了分页信息
		Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize());
	
		List<AdvertisementEntity> list = advertisementService.queryListByPage(parameters);
		 
		parameters.clear();
		parameters.put("isSuccess", Boolean.TRUE);
		parameters.put("nowPage", pager.getNowPage());
		parameters.put("pageSize", pager.getPageSize());
		parameters.put("pageCount", page.getPages());
		parameters.put("recordCount", page.getTotal());
		parameters.put("startRecord", page.getStartRow());
		//列表展示数据
		parameters.put("exhibitDatas", list);
		return parameters;
	}
	}
	//添加显示
	@RequestMapping("addUI.html")
	public String addUI(Model model, HttpServletRequest request) {
		 
		try
		{
			Bdictionary  bdictionary =new Bdictionary();
			bdictionary.setType("AD_RES_TYPE");
			List<Bdictionary> rstypes=advertisementService.querytype(bdictionary);
			model.addAttribute("rstype_list",rstypes);
			bdictionary.setType("AD_TYPE");
			List<Bdictionary> types=advertisementService.querytype(bdictionary);
			model.addAttribute("type_list",types);
			bdictionary.setType("AD_LEVEL");
			List<Bdictionary> levels=advertisementService.querytype(bdictionary);
			model.addAttribute("level_list",levels);
			bdictionary.setType("DRAMA_TYPE");
			List<Bdictionary> stypes=advertisementService.querytype(bdictionary);
			model.addAttribute("stype_list",stypes);
			return Common.BACKGROUND_PATH + "/localad/form";
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		
	}
	@RequestMapping("resources.html")
	public String resources(Model model) {
		
		int count=advertisementService.queryAllSeries();
		PageUtil pageUtil =new PageUtil();
		pageUtil.setPageNum(0);
		pageUtil.setPageSize(15);
		List<SeriesEntity> list=advertisementService.querySeries(pageUtil, "");
		System.out.println("当页查询出："+list.size());
		model.addAttribute("videoResource_list",list);
		int totalPageCount;
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		
		System.out.println("剧集数："+count);
		return Common.BACKGROUND_PATH + "/localad/resources";
	}
	
	//添加
	@RequestMapping("add.html")
	@ResponseBody
	public Object add(AdvertisementEntity advertisementEntity ,String sDate,String eDate) throws ParseException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		 
		try
		{
			System.out.println(sDate);
			if(sDate!=null && sDate!=""){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				Date date=format.parse(sDate);
				advertisementEntity.setStartDate(date);
			}
			if(eDate!=null && eDate!=""){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				Date date=format.parse(eDate);
				advertisementEntity.setEndDate(date);
			}
			int count=advertisementService.insert(advertisementEntity);
			 
			if(count >0 )
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "添加成功");
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "添加失败");
			}
		}catch(ServiceException e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//修改显示
	@RequestMapping("editUI.html")
	public String editUI(Model model, HttpServletRequest request, Long id) {
		
		try
		{
			 
			AdvertisementEntity advertisementEntity=advertisementService.findById(id);
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
			
			Bdictionary  bdictionary =new Bdictionary();
			bdictionary.setType("AD_RES_TYPE");
			List<Bdictionary> rstypes=advertisementService.querytype(bdictionary);
			model.addAttribute("rstype_list",rstypes);
			bdictionary.setType("AD_TYPE");
			List<Bdictionary> types=advertisementService.querytype(bdictionary);
			model.addAttribute("type_list",types);
			bdictionary.setType("AD_LEVEL");
			List<Bdictionary> levels=advertisementService.querytype(bdictionary);
			model.addAttribute("level_list",levels);
			bdictionary.setType("DRAMA_TYPE");
			List<Bdictionary> stypes=advertisementService.querytype(bdictionary);
			model.addAttribute("stype_list",stypes);
			model.addAttribute("page", page);
			model.addAttribute("advertisementEntity", advertisementEntity);
			Date date =advertisementEntity.getStartDate();
			if(date!=null){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				String sdate=format.format(date);
				model.addAttribute("sdate",sdate);
			}
				date =advertisementEntity.getEndDate();
			if(date!=null){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				String edate=format.format(date);
				model.addAttribute("edate",edate);
			}
			return Common.BACKGROUND_PATH + "/localad/form";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	//修改
	@RequestMapping("edit.html")
	@ResponseBody
	public Object update(AdvertisementEntity advertisementEntity,String sDate,String eDate)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		
		try
		 {  	 
				System.out.println("==========="+advertisementEntity.getTypeName());
			if(sDate!=null && sDate!=""){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				Date date=format.parse(sDate);
				advertisementEntity.setStartDate(date);
			}
			if(eDate!=null && eDate!=""){
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				Date date=format.parse(eDate);
				advertisementEntity.setEndDate(date);
			}
				int result = advertisementService.update(advertisementEntity);

			if(result >0)
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "编辑成功");
				
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "编辑失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//修改状态
		@RequestMapping("changestatus.html")
		@ResponseBody
		public Object updatestatus(AdvertisementEntity advertisementEntity)
		{
			Map<String, Object> map = new HashMap<String, Object>();
			
			try
			 {  	 
					
					System.out.println("ID++++++++"+advertisementEntity.getId());
					int result = advertisementService.updatestatus(advertisementEntity);

				if(result >0)
				{
					map.put("success", Boolean.TRUE);
					map.put("data", null);
					map.put("message", "编辑成功");
					
				}else
				{
					map.put("success", Boolean.FALSE);
					map.put("data", null);
					map.put("message", "编辑失败");
				}
			}catch(Exception e)
			{
				throw new AjaxException(e);
			}
			return map;
		}
	//删除
	@RequestMapping("deleteBatch.html")
	@ResponseBody
	public Object deleteBatch(String ids) {
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = advertisementService.deleteBatchById(list);
			System.out.println(cnt);
			if(cnt == list.size())
			{
				result.put("success", true);
				result.put("data", null);
				result.put("message", "删除成功");
			}else
			{
				result.put("success", false);
				result.put("data", null);
				result.put("message", "删除失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return result;
	}
	
	@RequestMapping(value="/searchPage.html", method={RequestMethod.GET,RequestMethod.POST})
	public String searchPage(Model model,VideoResourceEntity bSeries, String pSize,String pageNo,String sort,String name){
		PageUtil pageUtil =new PageUtil();
		int no=Integer.parseInt(pageNo)-1;
		pageUtil.setPageNum(no*Integer.parseInt(pSize));
		pageUtil.setPageSize(Integer.parseInt(pSize));
		System.out.println(sort);
		int totalPageCount;
		int count=advertisementService.queryAllSeries();
		if(count>15 ){
			totalPageCount=count/15;
			 if(count%15==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="";
		}
		List<SeriesEntity> list=advertisementService.querySeries(pageUtil, sort);
		model.addAttribute("videoResource_list", list);
		return Common.BACKGROUND_PATH+"/localad/resourceslist";
	}
 
	
}
